package com.sean.gulimall.product.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.sean.common.utils.PageUtils;
import com.sean.common.utils.R;
import com.sean.gulimall.product.entity.AttrGroupEntity;
import com.sean.gulimall.product.entity.CategoryEntity;
import com.sean.gulimall.product.service.AttrGroupService;
import com.sean.gulimall.product.service.CategoryService;
import com.sean.gulimall.product.vo.AttrRelationVo;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Map;



/**
 * 属性分组
 *
 * @author sean
 * @email sean_xin@126.com
 * @date 2022-05-22 21:41:06
 */
@RestController
@RequestMapping("product/attrgroup")
public class AttrGroupController {
    @Autowired
    private AttrGroupService attrGroupService;
    @Autowired
    private CategoryService categoryService;
    /**
     * 分组列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("product:attrgroup:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = attrGroupService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 分组信息
     */
    @RequestMapping("/info/{attrGroupId}")
    @RequiresPermissions("product:attrgroup:info")
    public R info(@PathVariable("attrGroupId") Long attrGroupId){
		AttrGroupEntity attrGroup = attrGroupService.getById(attrGroupId);
        //根据三级分类id获取到所有属性分组属性分类id
        Long[] path = categoryService.getCategoryPath(attrGroup.getCatelogId());
        attrGroup.setCategoryPath(path);
        return R.ok().put("attrGroup", attrGroup);
    }

    /**
     * 获取
     * @param categoryId
     * @return
     */
    private  Long getAllCategoryId(Long categoryId)
    {
        QueryWrapper<CategoryEntity> wrapper = new QueryWrapper<>();
        wrapper.lambda()
                .eq(CategoryEntity::getCatId,categoryId);
        CategoryEntity categoryEntity = categoryService.getOne(wrapper);
        if(categoryEntity != null && categoryEntity.getParentCid() != 0)
        {
            getAllCategoryId(categoryEntity.getParentCid());
        }
        return categoryEntity.getCatId();
    }




    /**
     * 保存
     */
    @RequestMapping("/save")
//    @RequiresPermissions("product:attrgroup:save")
    public R save(@RequestBody AttrGroupEntity attrGroup){
		attrGroupService.save(attrGroup);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("product:attrgroup:update")
    public R update(@RequestBody AttrGroupEntity attrGroup){
		attrGroupService.updateById(attrGroup);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("product:attrgroup:delete")
    public R delete(@RequestBody Long[] attrGroupIds){
		attrGroupService.removeByIds(Arrays.asList(attrGroupIds));

        return R.ok();
    }

    /**
     * 根据商品分类id获取属性分组列表
     * @param categoryId
     * @return
     */
    @GetMapping("/list/{categoryId}")
    public R getAttrGroupByCategoryId(@RequestParam Map<String, Object> params,@PathVariable("categoryId") Long categoryId){
        PageUtils page = attrGroupService.queryPageByCategoryId(params,categoryId);
        return R.ok().put("page", page);
    }

    /**
     * 根据属性分组id获取所有关联的属性
     * @param attrGroupId
     * @return
     */
    @GetMapping("/{attrGroupId}/attr/relation")
    public R getAllAttrListByAttrGroupId(@PathVariable("attrGroupId") Long attrGroupId){
        return attrGroupService.queryAllAttrByAttrGroupId(attrGroupId);
    }


    /**
     * 根据商品id获取所有分组及该分组下的所有属性
     * @param categoryId
     * @return
     */
    @GetMapping("/{categoryId}/withattr")
    public R getAttrByCategoryId(@PathVariable("categoryId") Long categoryId)
    {
        return attrGroupService.getAttrGroupWithAttrsByCatelogId(categoryId);
    }

    /**
     * 删除属性分组关联关系
     * @param attrRelationArray
     * @return
     */
    @PostMapping("/attr/relation/delete")
    public R deleteRelation(@RequestBody AttrRelationVo[] attrRelationArray)
    {
        return attrGroupService.removeRelation(attrRelationArray);
    }

    /**
     * 获取没有属性关联关系的基本属性
     * @param attrGroupId 属性分组id
     * @param params 查询参数，包括分页查询参数
     * @return
     */
    @GetMapping("/{attrGroupId}/noattr/relation")
    public R attrNoRealateList(@RequestParam Map<String, Object> params,@PathVariable("attrGroupId") Long attrGroupId)
    {
        return  attrGroupService.queryAllAttrNoRelationByAttrGroupId(params,attrGroupId);
    }

    @PostMapping("/attr/relation")
    public R addRelation(@RequestBody AttrRelationVo[] attrRelationArray)
    {
        return attrGroupService.addRelation(attrRelationArray);
    }

}
